﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SuperSprintZ
{
    /// <summary>
    /// Class for handling player input
    /// </summary>
    class PlayerController
    {
        private KeyboardState nappis;
        private KeyboardState prevnappis;
        private Player player;
        private Keys up;
        private Keys down;
        private Keys left;
        private Keys right;

        public PlayerController(Player player, Keys u, Keys d, Keys l, Keys r)
        {
            this.player = player;
            up = u;
            down = d;
            left = l;
            right = r;
        }

        public void Update(GameTime gameTime)
        {
            prevnappis = nappis;
            nappis = Keyboard.GetState();

            //Acceleration
            if (!player.game1.pause)
            {
                if (nappis.IsKeyDown(up))
                {
                    if (player.bPlayer1)
                    {
                        player.adios();
                    }
                    if (player.nopeus < 7)
                    {
                        player.nopeus += 0.08;
                    }
                }

                //Deacceleration
                if (nappis.IsKeyDown(down))
                {
                    if (player.bPlayer1)
                    {
                    }
                    if (player.nopeus >= 0)
                    {
                        player.nopeus -= 0.1;
                    }
                    else
                    {
                        if (player.nopeus > -2)
                        {
                            player.nopeus -= 0.04;
                        }
                    }
                }

                //Turning right
                if (nappis.IsKeyDown(right))
                {
                    player.suunta = player.suunta + player.kaannosnopeus;
                }

                //Turning left
                if (nappis.IsKeyDown(left))
                {
                    player.suunta = player.suunta - player.kaannosnopeus;
                }
            } //end of if !pause

            if (nappis.IsKeyDown(Keys.Escape))
            {
                this.player.game1.bMainmenu = true;
                //this.player.game1.Exit();
            }
            if (nappis.IsKeyDown(Keys.Pause) && !prevnappis.IsKeyDown(Keys.Pause) && player.bPlayer1) //only the other player's pause counts as one since both are using the same keyboard
            {
                if (player.game1.pause)
                {
                    MediaPlayer.Resume();
                    player.game1.pause = false;

                }
                else
                {
                    MediaPlayer.Pause();
                    player.game1.pause = true;
                }
               // player.game1.pause = !player.game1.pause;
            }
        }
    }
}